<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row v-show="showSearch">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
					<el-form-item label="合同编号" prop="contractNumber">
						<el-input placeholder="请输入合同编号" v-model="state.queryForm.contractNumber" />
					</el-form-item>
					<el-form-item label="合同名称" prop="contractName">
						<el-input placeholder="请输入合同名称" v-model="state.queryForm.contractName" />
					</el-form-item>
					<el-form-item label="合同类型" prop="contractType">
						<el-select v-model="state.queryForm.contractType" placeholder="请选择合同类型">
							<el-option :label="item.label" :value="item.value" v-for="(item, index) in contract_type" :key="index"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="项目编号" prop="projectNumber">
						<el-input placeholder="请输入项目编号" v-model="state.queryForm.projectNumber" />
					</el-form-item>
					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList"> 查询 </el-button>
						<el-button icon="Refresh" @click="resetQuery">重置</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'oa_projectContract_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column prop="contractNumber" label="合同编号" width="185" show-overflow-tooltip />
				<el-table-column prop="contractName" label="合同名称" show-overflow-tooltip />
				<el-table-column prop="contractType" label="合同类型" show-overflow-tooltip>
					<template #default="scope">
						<dict-tag :options="contract_type" :value="scope.row.contractType"></dict-tag>
					</template>
				</el-table-column>
				<el-table-column prop="contractMoney" label="合同金额" show-overflow-tooltip />
				<el-table-column prop="signDate" label="签订日期" show-overflow-tooltip />
				<el-table-column prop="circulationDate" label="流转日期" show-overflow-tooltip />
				<el-table-column prop="projectNumber" label="项目编号" show-overflow-tooltip />
				<el-table-column prop="bidServiceFeeInvoice" label="附件" show-overflow-tooltip>
					<template #default="scope">
						<div v-for="item in scope.row.fileList" :key="item.id">
							<a class="cursor-pointer" @click.prevent="downloadFile(item)">{{ item.original }}</a>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button icon="Collection" text type="primary" v-auth="'oa_projectContract_edit'" @click="handleDialog(scope.row.id)"></el-button>
						<el-button icon="edit-pen" text type="primary" v-auth="'oa_projectContract_edit'" @click="formDialogRef.openDialog(scope.row.id)"
							>编辑
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />

		<!-- 合同计划列表弹窗 -->
		<detail-list-dialog ref="detailListRef" />
	</div>
</template>

<script setup lang="ts" name="systemProjectContract">
import { BasicTableProps, useTable } from '@/hooks/table';
import { fetchList } from '@/api/oa/projectContract';
import { useDict } from '@/hooks/dict';
import other from '@/utils/other';

const { contract_type } = useDict('contract_type');
// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const DetailListDialog = defineAsyncComponent(() => import('./detailList.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
const detailListRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const currentContractId = ref<string>();
provide('currentContractId', currentContractId);
const handleDialog = (id: string) => {
	currentContractId.value = id;
	detailListRef.value.openDetailListDialog(id);
};

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/oa/projectContract/export', Object.assign(state.queryForm, { ids: selectObjs }), '合同列表.xlsx');
};

const downloadFile = (item: any) => {
	other.downBlobFile(`/admin/sys-file/${item.bucketName}/${item.fileName}`, {}, item.original);
};
</script>
